package com.example.user_service.user_contorller;



import com.example.user_service.user_contorller.FService.AddressService;
import com.example.user_service.user_contorller.FService.UserService;
import com.example.user_service.vo.AddressVO;
import com.example.user_service.vo.UserDataVO;
import com.example.user_service.vo.UserVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author fcr
 * @date 2021-12-14 11:35
 **/

@RestController
public class FController {

    @Autowired
    AddressService addressService;

    @Autowired
    UserService userService;

    @PostMapping("/f_checkid")
    @CrossOrigin
    public boolean f_checkid(int userid,String idcard){
        boolean result=userService.f_checkid(userid,idcard);
        if (result){
            return true;
        }
        return false;
    }

    @PostMapping("/f_login")
    @CrossOrigin
    public boolean f_login(int userid,String paswd){
        System.out.println(userid+"=="+paswd);
        boolean result=userService.f_login(userid,paswd);
        if (result){
            return true;
        }
        return false;
    }

    @PostMapping("/updateemail")
    @CrossOrigin
    public String updateemail(int userid,String newemail) {
        System.out.println(userid + "==" + newemail);
        return userService.updateemail(userid, newemail);
    }

    @PostMapping("/updatephone")
    @CrossOrigin
    public boolean updatephone(int userid,String tel){
        System.out.println(userid+"=="+tel);
        boolean result=userService.updatephone(userid,tel);
        if (result){
            return true;
        }
        return false;
    }

    @GetMapping("/selectmydata")
    @CrossOrigin
    public UserVO selectmydata(int userid){
        return userService.selectmydata(userid);
    }

    @GetMapping("/changedefault")
    @CrossOrigin
    public boolean changedefault(int addressid,int user_id){
        int result=addressService.changedefault(addressid,user_id);
        if (result==0){
            return false;
        }
        return true;
    }

    @GetMapping("/deleltadd")
    @CrossOrigin
    public boolean deleltadd(int addressid,int user_id){
        int result=addressService.deleltadd(addressid,user_id);
        if (result==0){
            return false;
        }
        return true;
    }

    @GetMapping("/selectaddressbyid")
    @CrossOrigin
    public AddressVO selectaddressbyid(int addressid){
        return addressService.selectaddressbyid(addressid);
    }

    @PostMapping("/addaddress")
    @CrossOrigin
    public boolean addaddress(AddressVO addressVO){
        //System.out.println(addressVO.getProvince()+"=="+addressVO.getCity()+"=="+addressVO.getAddresscode()+"=="+addressVO.getAddressdetailed()+"=="+addressVO.getAddressname()+"=="+addressVO.getStatus()+"=="+addressVO.getArea());
        int result=addressService.addaddress(addressVO);
        if (result==0){
            return false;
        }
        return true;
    }

    @PostMapping("/updateaddress")
    @CrossOrigin
    public boolean updateaddress(AddressVO addressVO){
        System.out.println(addressVO.getProvince()+"=="+addressVO.getCity()+"=="+addressVO.getAddresscode()+"=="+addressVO.getAddressdetailed()+"=="+addressVO.getAddressname()+"=="+addressVO.getStatus()+"=="+addressVO.getArea());
        int result=addressService.updateaddress(addressVO);
        if (result==0){
            return false;
        }
        return true;
    }

    @GetMapping("/selectmyaddress")
    @CrossOrigin
    public List<AddressVO> selectmyaddress(int userid,int status){
        return addressService.selectmyaddress(userid,status);
    }

    @PostMapping("/updatepwd")
    @CrossOrigin
    public boolean updatepwd(int userid,String paswd){
        int result=userService.updatepwd(paswd,userid);
        if (result==0){
            return false;
        }
        return true;
    }

    @GetMapping("/datachange")
    @CrossOrigin
    public boolean datachange(String account,String user_zsxm,String user_birthday,int user_gender,String user_area,int user_id) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM//
        Date date = simpleDateFormat.parse(user_birthday);
        System.out.println(account+"=="+user_zsxm+"=="+date+"=="+user_gender+"=="+user_area+"=="+user_id);
        UserVO newdata=new UserVO();
        newdata.setUser_id(user_id);
        newdata.setAccount(account);
        UserDataVO newuserdata=new UserDataVO();
        newuserdata.setUser_zsxm(user_zsxm);
        newuserdata.setUser_birthday(date);
        newuserdata.setUser_gender(user_gender);
        newuserdata.setUser_area(user_area);
        return userService.datachange(newdata,newuserdata);
    }

    /**上传图片**/
    @ApiOperation("上传头像")
    @RequestMapping("/changeheadimg")
    public String uploadFile(@RequestParam("avatar_file") MultipartFile avatar_file, HttpServletRequest request,int user_id)  {
        //判断文件是否为空
        if (avatar_file.isEmpty()) {
            return "上传文件不可为空";
        }

        // 获取文件名
        String fileName = avatar_file.getOriginalFilename();
        //使用时间戳加文件名作为新文件名
        fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) +fileName;


        //保存的路径
        String staticPath = System.getProperty("user.dir")+"\\user_service\\src\\main\\resources\\static\\img\\headimg\\";

        //文件的具体路径
        staticPath=staticPath+fileName;



        //创建文件路径

        File dest = new File(staticPath);
        //判断文件是否已经存在
        if (dest.exists()) {
            return "文件已经存在";
        }
        //判断文件父目录是否存在
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdir();
        }
        try {
            //上传文件//保存文件
            avatar_file.transferTo(dest);

        } catch (Exception e) {
            return "修改失败";
        }
        //服务器路径
        String serverUrl=request.getRequestURL().toString().replace(request.getRequestURI(),"");
        int result=userService.changehead(serverUrl+"/img/headimg/"+fileName,user_id);
        if (result!=0){
            return "头像修改成功";
        }
        //返回可访问的路径serverUrl+"/img/headimg/"+fileName
        return "修改失败";
    }


}
